
package gui;

import business.Categoria;
import business.Veiculo;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import vo.CategoriaVO;


public class Veiculo_Principal extends javax.swing.JPanel {
    
private static Veiculo_Principal instance;

    void setjComboBoxVeiculoPrincipalCategoria(String categoria) {
        jComboBoxVeiculoPrincipalCategoria.setSelectedItem(categoria);
    }

    public enum StatusVeiculoPrincipal{
        scInserindo,
        scPesquisado
    }
    
    private StatusVeiculoPrincipal sStatus;
   
    private Veiculo_Principal() {
        sStatus=StatusVeiculoPrincipal.scInserindo;
        initComponents();
        updateCategoria();
        
    }
    
    public static Veiculo_Principal getInstance(){
        if(instance == null)
            instance = new Veiculo_Principal();
        return instance;
    }
   
    public StatusVeiculoPrincipal getsStatus(){
        return sStatus;
    }
    
    public void setsStatus(StatusVeiculoPrincipal sStatus){
        this.sStatus = sStatus;
    }
    
    public void setjTextFieldVeiculoPrincipalPlaca(String placa){
       // System.out.println("placa funcao fieldplaca: "+placa);
        this.jTextFieldVeiculoPrincipalPlaca.setText(placa);
           
    }
        public String getjTextFieldVeiculoPrincipalPlaca(){
       // System.out.println("placa funcao fieldplaca: "+placa);
        return jTextFieldVeiculoPrincipalPlaca.getText();
           
    }
    
    public void setjTextFieldVeiculoPrincipalMarca(String marca){
              //  System.out.println("placa funcao fieldmarca: "+marca);
        this.jTextFieldVeiculoPrincipalMarca.setText(marca);
    }
    public void setjTextFieldVeiculoPrincipalModelo(String modelo){
        this.jTextFieldVeiculoPrincipalModelo.setText(modelo);
    }    
    public void setjTextFieldVeiculoPrincipalAno(String ano){
        this.jTextFieldVeiculoPrincipalAno.setText(ano);
    }    

    public void setjTextFieldVeiculoPrincipalKilometragem(String kilometragem){
        this.jTextFieldVeiculoPrincipalKilometragem.setText(kilometragem);
    } 
    public void setjTextFieldVeiculoPrincipalFoto(String fotoPath){
        this.jTextFieldVeiculoPrincipalFoto.setText(fotoPath);
    } 

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldVeiculoPrincipalPlaca = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldVeiculoPrincipalModelo = new javax.swing.JTextField();
        jTextFieldVeiculoPrincipalMarca = new javax.swing.JTextField();
        jTextFieldVeiculoPrincipalAno = new javax.swing.JTextField();
        jTextFieldVeiculoPrincipalKilometragem = new javax.swing.JTextField();
        jButtonVeiculoPrincipalFoto = new javax.swing.JButton();
        jButtonVeiculoPrincipalNovo = new javax.swing.JButton();
        jButtonVeiculoPrincipalPesquisar = new javax.swing.JButton();
        jButtonVeiculoPrincipalEditar = new javax.swing.JButton();
        jButtonVeiculoPrincipalDeletar = new javax.swing.JButton();
        jTextFieldVeiculoPrincipalFoto = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxVeiculoPrincipalCategoria = new javax.swing.JComboBox();
        jButtonVeiculoPrincipalSalvar = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(542, 241));
        setName(""); // NOI18N

        jTextFieldVeiculoPrincipalPlaca.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldVeiculoPrincipalPlacaFocusLost(evt);
            }
        });

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/Bundle"); // NOI18N
        jLabel1.setText(bundle.getString("Veiculo_Principal.jLabel1.text")); // NOI18N

        jLabel3.setText(bundle.getString("Veiculo_Principal.jLabel3.text")); // NOI18N

        jLabel4.setText(bundle.getString("Veiculo_Principal.jLabel4.text")); // NOI18N

        jLabel5.setText(bundle.getString("Veiculo_Principal.jLabel5.text")); // NOI18N

        jLabel6.setText(bundle.getString("Veiculo_Principal.jLabel6.text")); // NOI18N

        jLabel8.setText(bundle.getString("Veiculo_Principal.jLabel8.text")); // NOI18N

        jTextFieldVeiculoPrincipalModelo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldVeiculoPrincipalModeloFocusLost(evt);
            }
        });

        jTextFieldVeiculoPrincipalMarca.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldVeiculoPrincipalMarcaFocusLost(evt);
            }
        });

        jTextFieldVeiculoPrincipalKilometragem.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldVeiculoPrincipalKilometragemFocusLost(evt);
            }
        });

        jButtonVeiculoPrincipalFoto.setText(bundle.getString("Veiculo_Principal.jButtonVeiculoPrincipalFoto.text")); // NOI18N
        jButtonVeiculoPrincipalFoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeiculoPrincipalFotoActionPerformed(evt);
            }
        });

        jButtonVeiculoPrincipalNovo.setText(bundle.getString("Veiculo_Principal.jButtonVeiculoPrincipalNovo.text")); // NOI18N
        jButtonVeiculoPrincipalNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeiculoPrincipalNovoActionPerformed(evt);
            }
        });

        jButtonVeiculoPrincipalPesquisar.setText(bundle.getString("Veiculo_Principal.jButtonVeiculoPrincipalPesquisar.text")); // NOI18N
        jButtonVeiculoPrincipalPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeiculoPrincipalPesquisarActionPerformed(evt);
            }
        });

        jButtonVeiculoPrincipalEditar.setText(bundle.getString("Veiculo_Principal.jButtonVeiculoPrincipalEditar.text")); // NOI18N
        jButtonVeiculoPrincipalEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeiculoPrincipalEditarActionPerformed(evt);
            }
        });

        jButtonVeiculoPrincipalDeletar.setText(bundle.getString("Veiculo_Principal.jButtonVeiculoPrincipalDeletar.text")); // NOI18N
        jButtonVeiculoPrincipalDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeiculoPrincipalDeletarActionPerformed(evt);
            }
        });

        jLabel2.setText(bundle.getString("Veiculo_Principal.jLabel2.text")); // NOI18N

        jComboBoxVeiculoPrincipalCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<none>" }));

        jButtonVeiculoPrincipalSalvar.setText(bundle.getString("Veiculo_Principal.jButtonVeiculoPrincipalSalvar.text")); // NOI18N
        jButtonVeiculoPrincipalSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeiculoPrincipalSalvarActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel9.setText(bundle.getString("Veiculo_Principal.jLabel9.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonVeiculoPrincipalEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonVeiculoPrincipalPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButtonVeiculoPrincipalSalvar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButtonVeiculoPrincipalNovo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButtonVeiculoPrincipalDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel8)
                            .addComponent(jLabel2))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldVeiculoPrincipalPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldVeiculoPrincipalModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldVeiculoPrincipalKilometragem, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jComboBoxVeiculoPrincipalCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jTextFieldVeiculoPrincipalFoto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldVeiculoPrincipalMarca, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel6)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButtonVeiculoPrincipalFoto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextFieldVeiculoPrincipalAno, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(305, 305, 305)
                        .addComponent(jLabel9)
                        .addGap(0, 298, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel9)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldVeiculoPrincipalPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jTextFieldVeiculoPrincipalModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldVeiculoPrincipalMarca)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldVeiculoPrincipalAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldVeiculoPrincipalKilometragem)
                            .addComponent(jLabel5)
                            .addComponent(jComboBoxVeiculoPrincipalCategoria))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldVeiculoPrincipalFoto)
                            .addComponent(jButtonVeiculoPrincipalFoto))
                        .addGap(63, 63, 63))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonVeiculoPrincipalNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonVeiculoPrincipalSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonVeiculoPrincipalEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonVeiculoPrincipalPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jButtonVeiculoPrincipalDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(66, 66, 66))
        );

        getAccessibleContext().setAccessibleDescription("");
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonVeiculoPrincipalFotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeiculoPrincipalFotoActionPerformed
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "JPG & GIF Images", "jpg", "gif");
        chooser.setFileFilter(filter);
       
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) 
            this.jTextFieldVeiculoPrincipalFoto.setText(chooser.getSelectedFile().getAbsolutePath());
        
    }//GEN-LAST:event_jButtonVeiculoPrincipalFotoActionPerformed

    private void jButtonVeiculoPrincipalEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeiculoPrincipalEditarActionPerformed
         if (sStatus != StatusVeiculoPrincipal.scPesquisado)
             JOptionPane.showMessageDialog(this, "Pesquise um veículo para editá-lo.");
   
         else{
            Veiculo.alteraVeiculo(jTextFieldVeiculoPrincipalPlaca.getText(),jTextFieldVeiculoPrincipalKilometragem.getText(),jTextFieldVeiculoPrincipalFoto.getText());
            jTextFieldVeiculoPrincipalPlaca.setEnabled(true);
            jTextFieldVeiculoPrincipalMarca.setEnabled(true);
            jTextFieldVeiculoPrincipalModelo.setEnabled(true);
           
            jTextFieldVeiculoPrincipalAno.setEnabled(true);
            //jTextFieldVeiculoPrincipalChassis.setEnabled(true);
            jComboBoxVeiculoPrincipalCategoria.setEnabled(true);
            sStatus = StatusVeiculoPrincipal.scInserindo;
            LimpaTela();
         }
    }//GEN-LAST:event_jButtonVeiculoPrincipalEditarActionPerformed

    private void jButtonVeiculoPrincipalPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeiculoPrincipalPesquisarActionPerformed
       Veiculo_Pesquisar Frame=new Veiculo_Pesquisar();
       Frame.setVisible(true); 
    }//GEN-LAST:event_jButtonVeiculoPrincipalPesquisarActionPerformed

   void setjTextFieldVeiculoPrincipalPlacaEnabled(boolean enable) {
         jTextFieldVeiculoPrincipalPlaca.setEnabled(enable);
    }
   void setjTextFieldVeiculoPrincipalModeloEnabled(boolean enable) {
         jTextFieldVeiculoPrincipalModelo.setEnabled(enable);
    } 
   void setjTextFieldVeiculoPrincipalMarcaEnabled(boolean enable) {
         jTextFieldVeiculoPrincipalMarca.setEnabled(enable);
    } 

      void setjTextFieldVeiculoPrincipalAnoEnabled(boolean enable) {
         jTextFieldVeiculoPrincipalAno.setEnabled(enable);
    }  
      void setjComboBoxVeiculoPrincipalCategoriaEnabled(boolean enable) {
         jComboBoxVeiculoPrincipalCategoria.setEnabled(enable);
    }   
    private void jButtonVeiculoPrincipalNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeiculoPrincipalNovoActionPerformed
        LimpaTela();
        jTextFieldVeiculoPrincipalPlaca.setEnabled(true);
        jTextFieldVeiculoPrincipalMarca.setEnabled(true);
        jTextFieldVeiculoPrincipalModelo.setEnabled(true);
        jComboBoxVeiculoPrincipalCategoria.setEnabled(true);
        jTextFieldVeiculoPrincipalAno.setEnabled(true);
        sStatus = StatusVeiculoPrincipal.scInserindo;
    }//GEN-LAST:event_jButtonVeiculoPrincipalNovoActionPerformed

    private void jButtonVeiculoPrincipalDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeiculoPrincipalDeletarActionPerformed
         if (sStatus != StatusVeiculoPrincipal.scPesquisado)
             JOptionPane.showMessageDialog(this, "Pesquise um veículo para deletá-lo.");
         else{
            Veiculo.deleteVeiculoPelaPlaca(jTextFieldVeiculoPrincipalPlaca.getText());
            jTextFieldVeiculoPrincipalPlaca.setEnabled(true);
            jTextFieldVeiculoPrincipalMarca.setEnabled(true);
            jTextFieldVeiculoPrincipalModelo.setEnabled(true);
            jComboBoxVeiculoPrincipalCategoria.setEnabled(true);
            jTextFieldVeiculoPrincipalAno.setEnabled(true);
            
            sStatus = StatusVeiculoPrincipal.scInserindo;
            LimpaTela();
         }
    }//GEN-LAST:event_jButtonVeiculoPrincipalDeletarActionPerformed

    private void jButtonVeiculoPrincipalSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeiculoPrincipalSalvarActionPerformed

        if (sStatus == StatusVeiculoPrincipal.scInserindo){
             if (jTextFieldVeiculoPrincipalPlaca.getText().isEmpty()    ||
                jTextFieldVeiculoPrincipalMarca.getText().isEmpty()   || jTextFieldVeiculoPrincipalKilometragem.getText().isEmpty() ||
                jTextFieldVeiculoPrincipalModelo.getText().isEmpty()||jTextFieldVeiculoPrincipalFoto.getText().isEmpty()||
                jTextFieldVeiculoPrincipalAno.getText().isEmpty() || jComboBoxVeiculoPrincipalCategoria.getSelectedItem()=="" 
                ){
                    JOptionPane.showMessageDialog(this,"Existem campos em branco!");
                    return;
            }
        //TODO Verificar se veículo já existe
      
           Veiculo.insertVeiculo(jTextFieldVeiculoPrincipalPlaca.getText(),jTextFieldVeiculoPrincipalMarca.getText(),
                        jTextFieldVeiculoPrincipalModelo.getText(),jTextFieldVeiculoPrincipalAno.getText(),Integer.parseInt(jTextFieldVeiculoPrincipalKilometragem.getText())
                        ,(String)jComboBoxVeiculoPrincipalCategoria.getSelectedItem(),false,jTextFieldVeiculoPrincipalFoto.getText());
                        LimpaTela();
        }
        else{
            JOptionPane.showMessageDialog(this, "Para inserir o num novo veiculo, clique em novo.");
        }   
    }//GEN-LAST:event_jButtonVeiculoPrincipalSalvarActionPerformed

    private void jTextFieldVeiculoPrincipalPlacaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldVeiculoPrincipalPlacaFocusLost
            String   placa=jTextFieldVeiculoPrincipalPlaca.getText();
     
      if(!Veiculo.PlacaValida(placa)||"".equals(placa)||Veiculo.VeiculoExiste(placa))
      {
                JOptionPane.showMessageDialog(this, "Placa inválida.");
                jTextFieldVeiculoPrincipalPlaca.requestFocus();
                jTextFieldVeiculoPrincipalPlaca.selectAll(); 
                
                return;
      }    
            
    }//GEN-LAST:event_jTextFieldVeiculoPrincipalPlacaFocusLost

    private void jTextFieldVeiculoPrincipalModeloFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldVeiculoPrincipalModeloFocusLost
    
      String Model = jTextFieldVeiculoPrincipalModelo.getText();
      
      if("".equals(Model))
      {
        JOptionPane.showMessageDialog(this, "Modelo inválida.");
        jTextFieldVeiculoPrincipalModelo.requestFocus();
        jTextFieldVeiculoPrincipalModelo.selectAll();    
                return;
      }  
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldVeiculoPrincipalModeloFocusLost

    private void jTextFieldVeiculoPrincipalMarcaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldVeiculoPrincipalMarcaFocusLost
         String Marca = jTextFieldVeiculoPrincipalModelo.getText();
      
      if("".equals(Marca))
      {
        JOptionPane.showMessageDialog(this, "Marca inválida.");
        jTextFieldVeiculoPrincipalMarca.requestFocus();
        jTextFieldVeiculoPrincipalMarca.selectAll();    
                return;
      }  
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldVeiculoPrincipalMarcaFocusLost

    private void jTextFieldVeiculoPrincipalKilometragemFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldVeiculoPrincipalKilometragemFocusLost
     String KM=jTextFieldVeiculoPrincipalKilometragem.getText();
     
     for(int i=0;i<KM.length();i++)
     {
         if(!Character.isDigit(KM.charAt(i)))
         {
            JOptionPane.showMessageDialog(this, "Quilometragem inválida.");
            jTextFieldVeiculoPrincipalKilometragem.requestFocus();
            jTextFieldVeiculoPrincipalKilometragem.selectAll();    
                return;            
     }}
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldVeiculoPrincipalKilometragemFocusLost

     private void updateCategoria(){
        
        List<CategoriaVO> lista = Categoria.getCategorias();
        
        String[] dados = new String[lista.size()];
        
        for(int i=0;i<lista.size();i++)
        {
            CategoriaVO categoria = (CategoriaVO) lista.get(i);
            dados[i] = categoria.getNome();
            ((DefaultComboBoxModel)jComboBoxVeiculoPrincipalCategoria.getModel()).addElement(dados[i]);
        }

    }
     
    private void LimpaTela(){
       jTextFieldVeiculoPrincipalPlaca.setText("");
        jTextFieldVeiculoPrincipalMarca.setText("");
        jTextFieldVeiculoPrincipalModelo.setText("");
        jComboBoxVeiculoPrincipalCategoria.setSelectedIndex(0);
        jTextFieldVeiculoPrincipalAno.setText("");
        //jTextFieldVeiculoPrincipalChassis.setText("");
        jTextFieldVeiculoPrincipalKilometragem.setText("");
        jTextFieldVeiculoPrincipalFoto.setText("");
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonVeiculoPrincipalDeletar;
    private javax.swing.JButton jButtonVeiculoPrincipalEditar;
    private javax.swing.JButton jButtonVeiculoPrincipalFoto;
    private javax.swing.JButton jButtonVeiculoPrincipalNovo;
    private javax.swing.JButton jButtonVeiculoPrincipalPesquisar;
    private javax.swing.JButton jButtonVeiculoPrincipalSalvar;
    private javax.swing.JComboBox jComboBoxVeiculoPrincipalCategoria;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextFieldVeiculoPrincipalAno;
    private javax.swing.JTextField jTextFieldVeiculoPrincipalFoto;
    private javax.swing.JTextField jTextFieldVeiculoPrincipalKilometragem;
    private javax.swing.JTextField jTextFieldVeiculoPrincipalMarca;
    private javax.swing.JTextField jTextFieldVeiculoPrincipalModelo;
    private javax.swing.JTextField jTextFieldVeiculoPrincipalPlaca;
    // End of variables declaration//GEN-END:variables
}
